from django.db import models
from django.utils.translation import ugettext_lazy as _
from djiape.entities.models import Currency, Tax
from django.contrib import admin

class Family(models.Model):
    id = models.CharField(_('id'), max_length=10, primary_key=True)
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('familiy')
        verbose_name_plural = _('families')
    
admin.site.register(Family)
    
class Unit(models.Model):
    id = models.CharField(_('id'), max_length=3, primary_key=True)
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('unit')
        verbose_name_plural = _('units')
    
admin.site.register(Unit)
    
class Product(models.Model):
    id = models.CharField(_('id'), max_length=15, primary_key=True)
    name = models.CharField(_('name'), max_length=40)
    description = models.TextField(_('description'), blank=True, null=True)
    family = models.ForeignKey(Family, verbose_name=_('family'))
    a_unit = models.ForeignKey(Unit, verbose_name=_('adquisition unit'), related_name='aunit')
    s_unit = models.ForeignKey(Unit, verbose_name=_('sales unit'), related_name='sunit')
    unit_factor = models.DecimalField(_('convertion factor'), max_digits=14, decimal_places=4, default=1)
    base_price = models.DecimalField(_('price'), max_digits=14, decimal_places=2)
    a_currency = models.ForeignKey(Currency, verbose_name=_('adquisition currency'), related_name='acurrency')
    s_currency = models.ForeignKey(Currency, verbose_name=_('sales currency'), related_name='scurrency')
    cost = models.DecimalField(_('cost'), max_digits=14, decimal_places=2, default=0)
    tax = models.ForeignKey(Tax, verbose_name=_('tax'))
    manage_control = models.BooleanField(_('manage control?'), default=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('product')
        verbose_name_plural = _('products')
    
admin.site.register(Product)
    
class ListOfPrices(models.Model):
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('list of prices')
        verbose_name_plural = _('lists of prices')
    
admin.site.register(ListOfPrices)
    
class Prices(models.Model):
    product = models.ForeignKey(Product, verbose_name=_('product'))
    type = models.ForeignKey(ListOfPrices, verbose_name=_('type'))
    price = models.DecimalField(_('price'), max_digits=14, decimal_places=2)
    def __unicode__(self):
        return '%s %s' % (self.product, self.type)
    class Meta:
        ordering = ["product", "type"]
        verbose_name = _('price')
        verbose_name_plural = _('prices')
    
admin.site.register(Prices)
    
